package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.lang.StringUtils;
import javax.servlet.http.HttpServletRequest;

import java.time.LocalDateTime;



@Slf4j
@Controller
@ResponseBody
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
        String password = employee.getPassword();
        //将login页面提交的密码进行加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);
        //查询为空用户名不存在
        if (emp==null){
            return R.error("登录失败");
        }
        //查询密码错误
        if (!emp.getPassword().equals(password)){
            return R.error("密码错误");
        }
        //status结果为1账号可以使用，为0账号不能使用
        if (emp.getStatus() == 0){
            return R.error("账号已禁用");
        }
        //登录成功，将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }
    /**
     * 员工退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request ){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee ,HttpServletRequest request){
        log.info("新增员工，员工信息：{}",employee.toString());
        //设置初始密码123456对其进行加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

//        //创建时间
//        employee.setCreateTime(LocalDateTime.now());
//        //更新时间
//        employee.setUpdateTime(LocalDateTime.now());
//        //获取当前用户的id
//        Long empId = (Long) request.getSession().getAttribute("employee");
//
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);

        //向数据库插入用户
        employeeService.save(employee);

        return R.success("新增员工成功");
    }

    /**
     * 员工信息分页查询
     * @param page 为页面号
     * @param pageSize  页面展示信息的行数
     * @param name
     * @return
     */
    @GetMapping("/page")
    private R<Page> page(int page, int pageSize , String name){
        log.info("page = {},pageSize = {},name = {}",page,pageSize,name);

        Page pageInfo = new Page(page,pageSize);

        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper();
        //添加模糊查询条件
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //对查询结果按更新时间进行降序排列
        lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(pageInfo,lambdaQueryWrapper);

        return R.success(pageInfo);

    }

    /**
     * 根据id修改员工信息
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> upDate(@RequestBody Employee employee ,HttpServletRequest request){
        log.info(employee.toString());

//        //注入更新时间
//        employee.setUpdateTime(LocalDateTime.now());
//        //注入操作的用户
//        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);

        //执行更新操作
        employeeService.updateById(employee);

        return R.success("员工信息修改成功");
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        log.info("根据ID查询员工信息 "+id);
        //根据id查询信息
        Employee byId = employeeService.getById(id);
        if (byId == null){
            R.error("没有查询到对应员工信息");
        }
        return R.success(byId);
    }












}
